/**
 * Created by dukai on 2017/5/11.
 */
MineSettingController = function () {
    var self = this;
    this.getAppVersion = function () {
        AppUtil.call(AppUtil.APIS.GET_APP_VERSION, {}, function (result) {
            console.info(JSON.stringify(result));
            var $version = $('#mineSettingPage #aboutUs .right .value');
            if (result.success.toString() == "true") {
                $version.text('v' + result.resultObject);
                self.checkAppUpgrade(result.resultObject);
            } else {
                $version.text('');
                DialogUtil.dialogWarn("无法获取app版本");
            }
        });
    };
    this.checkAppUpgrade = function (appVersion) {
        debugger
        var isAndroid = false;
        var ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("android") >= 0) {
            isAndroid = true;
        }
        var param = { appVersion: appVersion, type: '3', platform: isAndroid ? '1' : '2', h5Module: {} };
        var serverUrl = App.serverUrl + "app/version/upgrade.do";
        var sucFunc = function (result) {
            if (result.success.toString() == "true") {
                var $new = $('#mineSettingPage #aboutUs .left');
                if (result.resultObject.success && result.resultObject.resultObject&&result.resultObject.resultObject.appVersion) {
                    $new.addClass('new');
                } else {
                    $new.removeClass('new');
                }
            }
        };
        var failFunc = function () {

        };
         //CommonUtil.sendJsonpRequest({"url":serverUrl,"params":param,"success":sucFunc,"error":failFunc});
        AppUtil.call(AppUtil.APIS.SEND_HTTP_REQUEST,
            {
                "data": JSON.stringify(param),
                "type": "POST",
                "url": serverUrl
            }, sucFunc);
    };
    this.sendVerifyCode = function (phoneNum) {

        var serverUrl = App.serverUrl + "app/setting/sendVerifyCodeModifyPhone.do";
        var sendVerifyCodeParams = { "phoneNum": phoneNum };

        var sucFunc = function (result) {

            /*if (result.success ) {
                DialogUtil.dialogSuccess("发送短信验证码成功！")
            } else {
                DialogUtil.dialogWarn(result.msg);
            }*/

            if (result.success) {
                if (result.resultObject.success) {

                    //倒计时60秒
                    /* if($(".get-code").hasClass('disabled')){
                         return;
                     }*/
                    commomWaitSecond({
                        second: 60,
                        target: $(".get-code"),
                        normalText: "获取验证码",
                        waiting: function (second) {
                            return second + "秒后重发";
                        }
                    });

                    // DialogUtil.dialogSuccess("发送短信验证码成功！")
                    DialogUtil.dialogWarn("发送短信验证码成功！");
                } else {
                    DialogUtil.dialogWarn(result.resultObject.msg);
                }
            } else {
                DialogUtil.dialogWarn(result.msg);
            }
        };

        /*var failFunc = function () {
            DialogUtil.dialogWarn();
        };*/

        // CommonUtil.sendJsonpRequest({"url":serverUrl,"params":sendVerifyCodeParams,"success":sucFunc,"error":failFunc});
        // CommonUtil.ajaxRequest(serverUrl, "POST", sendVerifyCodeParams , sucFunc, failFunc);

        AppUtil.call(AppUtil.APIS.SEND_HTTP_REQUEST,
            {
                "data": JSON.stringify(sendVerifyCodeParams),
                "type": "POST",
                "url": serverUrl
            }, sucFunc);

    };



    this.modifyPhone = function (password, phoneNum, verifyCode) {

        // common.loading("请稍后...");
        var serverUrl = App.serverUrl + "app/setting/modifyPhoneNum.do";
        var modifyPhoneParams = { "phoneNum": phoneNum, "verifyCode": verifyCode, "password": password };

        var sucFunc = function (result) {

            /*if (result.success) {
                // var data = result.resultObject;
                DialogUtil.dialogSuccess("修改手机号成功！")
                $.router.load("./mine-setting.html");
            } else {
                DialogUtil.dialogWarn(result.msg);
            }*/

            if (result.success) {
                if (result.resultObject.success) {
                    // DialogUtil.dialogSuccess("修改手机号成功！")
                    DialogUtil.dialogWarn("修改手机号成功！");

                    //让提示停留一会再跳转至首页
                    // common.loading();
                    // setTimeout(function () {
                    //     common.closeLoading();
                    // }, 1000);

                    AppUtil.call("logout", {}, function (result) {
                        if (result == -1 || result == '-1') {
                            DialogUtil.dialogWarn("退出登录失败！");
                        }
                    });

                } else {
                    DialogUtil.dialogWarn(result.resultObject.msg);
                }
            } else {
                DialogUtil.dialogWarn(result.msg);
            }
        };

        /* var failFunc = function () {
             DialogUtil.dialogWarn();
         };*/

        // CommonUtil.sendJsonpRequest({"url":serverUrl,"params":modifyPhoneParams,"success":sucFunc,"error":failFunc});
        // CommonUtil.ajaxRequest(serverUrl, "POST", modifyPhoneParams , sucFunc, failFunc);

        AppUtil.call(AppUtil.APIS.SEND_HTTP_REQUEST,
            {
                "data": JSON.stringify(modifyPhoneParams),
                "type": "POST",
                "url": serverUrl
            }, sucFunc);

    };



    this.modifyPwd = function (oldPassword, newPassword) {

        // common.loading("请稍后...");
        var serverUrl = App.serverUrl + "app/setting/modifyPwd.do";
        var modifyPwdParams = { "oldPassword": oldPassword, "newPassword": newPassword };

        var sucFunc = function (result) {

            /* if (result.success) {
                 // var data = result.resultObject;
                 DialogUtil.dialogSuccess("修改密码成功！")
                 $.router.load("./mine-setting.html");

             } else {
                 DialogUtil.dialogWarn(result.msg);
             }*/

            if (result.success) {
                if (result.resultObject.success) {
                    // DialogUtil.dialogSuccess("修改密码成功！");
                    DialogUtil.dialogWarn("修改密码成功！");

                    //让提示停留一会再跳转至首页
                    // common.loading();
                    // setTimeout(function () {
                    //     common.closeLoading();
                    // }, 1000);

                    AppUtil.call("logout", {}, function (result) {
                        if (result == -1 || result == '-1') {
                            DialogUtil.dialogWarn("退出登录失败！");
                        }
                    });

                } else {
                    DialogUtil.dialogWarn(result.resultObject.msg);
                }
            } else {
                DialogUtil.dialogWarn(result.msg);
            }

        };

        /*var failFunc = function () {
            DialogUtil.dialogWarn();
        };*/

        // CommonUtil.sendJsonpRequest({"url":serverUrl,"params":modifyPwdParams,"success":sucFunc,"error":failFunc});
        // CommonUtil.ajaxRequest(serverUrl, "POST", modifyPwdParams , sucFunc, failFunc);

        AppUtil.call(AppUtil.APIS.SEND_HTTP_REQUEST,
            {
                "data": JSON.stringify(modifyPwdParams),
                "type": "POST",
                "url": serverUrl
            }, sucFunc);

    };



    /* this.logout = function () {

         var serverUrl = App.serverUrl + "/app/setting/logout.do";
         // var logoutParams = {};

         var sucFunc = function (result) {

             if (result.success) {
                 // var data = result.resultObject;
                 // DialogUtil.dialogSuccess("退出成功！")

                 //TODO 跳到登录页面！！！
                 // $.router.load("./mine-setting.html");
                 AppUtil.call("logout");
                 // window.

             } else {
                 DialogUtil.dialogWarn(result.msg);
             }
         };

         var failFunc = function () {
             DialogUtil.dialogWarn();
         };

         CommonUtil.sendJsonpRequest({"url":serverUrl,"success":sucFunc,"error":failFunc});
         // CommonUtil.ajaxRequest(serverUrl, "POST", null , sucFunc, failFunc);

     };
 */

    /*this.dialNumber = function () {
        AppUtil.call(AppUtil.APIS.DIAL,{"mobile":"400123412312"});
    };*/
}
